Ext.namespace('Ext.ux', 'Ext.ux.plugins');

Ext.ux.plugins.HtmlEditorImageInsert = function(config) {

    config = config || {};
    
    Ext.apply(this, config);

    this.init = function(htmlEditor) {
        this.editor = htmlEditor;
        this.editor.on('render', onRender, this);
        this.editor.range = null;
    };

    this.imageInsertConfig = {
        popTitle: config.popTitle || 'Image URL',
        popMsg: config.popMsg || 'Please select the URL of the image you want to insert:',
        popWidth: config.popWidth || 350,
        popValue: config.popValue || ''
    }

    this.imageInsert = function(){
    		if(Ext.isIE){
        	if (this.editor.doc.selection.type == 'Control') {
      			this.editor.con = this.editor.doc.selection;
      			this.editor.range = null;
      			alert("Выберите текст, а не объект");
        	} else if (this.editor.doc.selection.type == 'Text') {
      			this.editor.con = null;
      			this.editor.range = this.editor.doc.selection.createRange();
      			this.showpicuploadform();
        	} else {
      			this.editor.con = null;
      			this.editor.range = this.editor.doc.selection.createRange();
      			this.showpicuploadform();
        	}
        } else {
        	this.showpicuploadform();
        }
        //this.editor.relayCmd('insertimage', text);
    }
		
		this.showpicuploadform = function(){
			Ext.get('picuploadform').dom.style.display = 'block';
			Ext.get('screen').dom.style.display = 'block';
		}
		
		insertImage = function() {
			hidepicuploadform();
			var htmlEditor = Ext.getCmp('content');
      if(Ext.isIE){
      		htmlEditor.range.pasteHTML("<img src='/img/page.gif'/>");
      }else if(Ext.isGecko || Ext.isOpera){
          htmlEditor.win.focus();
          htmlEditor.execCmd('InsertHTML', "<img src='/img/page.gif'/>");
          htmlEditor.deferFocus();
      }else if(Ext.isSafari){
          htmlEditor.execCmd('InsertText', "<img src='/img/page.gif'/>");
          htmlEditor.deferFocus();
      }
		}

    function onRender() {

        this.editor.tb.add({
            itemId : 'htmlEditorImage',
            cls : 'x-btn-icon x-edit-insertimage',
            enableToggle: false,
            tooltip: {
							cls: 'none',
							title: 'Image',
							text: 'Open insert image dialog.'
						},
            scope: this,
            handler:function(){ this.imageInsert(); },
            clickEvent:'mousedown',
            tabIndex:-1
        });

        var mainTabPanel = Ext.getCmp('mainTabPanelID');
        
        var el = mainTabPanel.getEl();
        var newElem = document.createElement('div');
				
        newElem.innerHTML='<script>' +
				'hidepicuploadform = function(){' +
				'Ext.get("picuploadform").dom.style.display="none";' +
				'Ext.get("screen").dom.style.display="none";' +
				'}' +
				'</scr'+'ipt>' +
				'<div id="picuploadform">' +
        '<div class="shadow">' +
        '<label>Загрузить картинку:</label>' +
        '<div id="iframe_container">' +
        '<iframe src="upload.htm" frameborder="0" scrolling="no"></iframe>' +
        '</div>' +
        '<div id="images_container"></div><br clear="all">' +
        '<form target="upframe" action="asfdasdfsadf" method="POST" enctype="multipart/form-data">' +
        '<label>Ширина (в пикселях):</label>' +
        '<div><input type="text" name="width" value="" style="width: 100px;"></div>' +
        '<label>Выравнивание:</label>' +
        '<div><select name="align"><option value="">нет <option value="left">слева<option value="center">по центру<option value="right">справа</select></div>' +
        '<label>Описание:</label><div><input type="text" name="alt" value="" style="width: 100%;"></div>' +
        '<div class="buttns">' +
        '  <input id="pb1" type="button" onClick="return insertImage();" value="Ога">' +
        '  <input id="pb2" type="button" onClick="return hidepicuploadform();" value="Закрыть">' +
        '  <span style="display: none;"><iframe src="about:blank" width="1" height="1" name="upframe"></iframe></span>' +
        '</div>' +
        '</form>' +
        '</div>' +
        '</div>' +
        '<div id="screen" onclick="hidepicuploadform();"></div>';
        
        el.appendChild(newElem);

    }

}